package com.noble.meether.chat.ui.agora.act.voice.girlver;

import android.content.Context;
import android.os.SystemClock;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.custom.RadiationViewBg;
import com.noble.library.image.ImageLoader;
import com.noble.library.utils.ScreenUtils;
import com.noble.library.view.anim.AnimationUtil;
import com.noble.library.view.base.BasePanel;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.model.EngineConfig;
import com.noble.meether.chat.ui.agora.util.RtcTimerUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 女性任务版： 等待页面底部panel
 */
public class RtcInitFootPanel extends BasePanel implements View.OnClickListener {
    private EngineConfig config;
    private TextView tv_waiting;
    private ImageView iv_head;

    private RadiationViewBg rvFirst, rvSec;

    public RtcInitFootPanel(Context context) {
        super(context);
        setContentView(R.layout.act_group_foot);
        initView();
    }

    private void initView() {
        initTipsList();
        initRadiationView();
        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        iv_head = (ImageView) findViewById(R.id.iv_head);
        ImageLoader.loadCircleAvatar(getContext(), userDetail.getAvatar(), iv_head);
        tv_waiting = (TextView) findViewById(R.id.tv_waiting);

        config = ModuleMgr.getRtcEnginMgr().getEngineConfig();
        findViewById(R.id.btn_hangup).setOnClickListener(this);
        Chronometer inviteCountUp = (Chronometer) findViewById(R.id.ch_inv_time);
        RtcTimerUtil.getInstance().addTimer(inviteCountUp);
        inviteCountUp.setBase(SystemClock.elapsedRealtime() - config.mInviteTime);
        inviteCountUp.start();
        startTipsAnim();
    }

    public void refreshView(UserLight info) {
        ImageLoader.loadCircleAvatar(getContext(), info.getAvatar(), iv_head);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_hangup:
                reqVCGroupCancel();
                getActivity().finish();
                break;
        }
    }

    /**
     * 取消群发邀请
     */
    public void reqVCGroupCancel() {
        config.isCancel = true;
        config.isCalling = false;
        ModuleMgr.getRtcChatMgr().reqChatCancel(config.mVcid, null);
    }

    private void initRadiationView() {
        rvFirst = (RadiationViewBg) findViewById(R.id.rv_first);
        rvSec = (RadiationViewBg) findViewById(R.id.rv_sec);
        rvFirst.setDpMultiple(ScreenUtils.toDpMultiple(getActivity()));
        rvSec.setDpMultiple(ScreenUtils.toDpMultiple(getActivity()));

        // 两个控件互相调起
        rvFirst.setRadiationListener(() -> {
            rvSec.startRadiate();   // rvSec开启
            if (rvSec.getVisibility() == View.GONE)
                rvSec.setVisibility(View.VISIBLE);
        });

        rvSec.setRadiationListener(() -> {
            rvFirst.startRadiate(); // rvFirst开启
        });
        rvFirst.startRadiate();  // 启动其中之一
    }

    // ============ 提示文字 ==================
    private List<String> tips = new ArrayList<>();

    private void initTipsList() {
        tips.add("正在努力推送中");
        tips.add("她会是谁呢？遇见中");
        tips.add("已有大量女生收到邀请");
    }

    private void startTipsAnim() {
        int tipsPosition = new Random().nextInt(tips.size());
        tv_waiting.setText(tips.get(tipsPosition));
        AnimationUtil.showAndHiddenCircle(tv_waiting, 1800, new AnimationUtil.AnimationCallback() {
            @Override
            public void onAnimStart() {
            }

            @Override
            public void onAnimRepeat() {
            }

            @Override
            public void onAnimEnd() {
                startTipsAnim();
            }
        });
    }
}